//
//  SPLoginViewController.m
//  Sport
//
//  Created by Cass on 2018/3/23.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPLoginViewController.h"
#import "SPRegisterViewController.h"
#import "SPTabBarController.h"
@interface SPLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;

@end

@implementation SPLoginViewController

//http://39.108.132.194/Sport/login.php
//accountName——账号
//password——密码

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    //读取上次的配置
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //保存账号
    self.phoneText.text = [userDefault objectForKey:@"phoneNumber"];
    //保存密码
    self.passwordText.text = [userDefault objectForKey:@"password"];
    
    if ([self.phoneText.text isEqualToString:@"18328700795"] && [self.passwordText.text isEqualToString:@"zp123"]) {
        [self login];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.passwordText setSecureTextEntry:YES];
}
//@"text/html",@"application/json", @"text/json", @"text/javascript", @"text/plain"
- (IBAction)loginBtnClick:(id)sender {
    [self login];
}

- (void)login{
    NSString *url = [NSString stringWithFormat:@"http://39.108.132.194/Sport/login.php"];
    AFHTTPSessionManager *maneger = [AFHTTPSessionManager manager];
    maneger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    maneger.requestSerializer = [AFHTTPRequestSerializer serializer];
    maneger.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *dict = @{
                           @"accountName":self.phoneText.text,
                           @"password":self.passwordText.text
                           };
    
    [maneger POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"--上传成功--%@",responseObject);
        [SVProgressHUD showWithStatus:@"登录中"];
        [SVProgressHUD dismissWithDelay:0.5];
        NSData *strData = responseObject;
        NSString *strInfo = [[NSString alloc]initWithData:strData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",strInfo);

        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:self.phoneText.text forKey:@"phoneNumber"];
        [userDefault setObject:self.passwordText.text forKey:@"password"];
        [userDefault synchronize];
        //延时处理
        dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
        dispatch_after(timer, dispatch_get_main_queue(), ^{
            [self showTabbarVC];
        });
//        [self performSelector:@selector(showTabbarVC) withObject:nil afterDelay:0.5];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"--上传失败--%@",error);
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器错误原因--%@",str);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)showTabbarVC{
    SPTabBarController *tabbarVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPTabBarController class])];
    //        [self.navigationController pushViewController:tabbarVC animated:YES];
    [self presentViewController:tabbarVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
